package com.yunxin.download.server.service.impl;

import com.yunxin.download.server.infra.repository.DownloadDataSourceConfigDao;
import com.yunxin.download.server.infra.support.datasource.DataSourceInitListener;
import com.yunxin.download.server.model.entity.DownloadDataSourceConfig;
import com.yunxin.download.server.service.DataSourceService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author zero°
 * @since 2023-06-13
 */
@Service
@RequiredArgsConstructor
public class DataSourceServiceImpl implements DataSourceService {

    private final DownloadDataSourceConfigDao dataSourceConfigDao;

    private final DataSourceInitListener dataSourceInitListener;

    @Override
    public String addDateSource(DownloadDataSourceConfig config) {
        Long id = dataSourceConfigDao.selectDataSourceIdBySourceNameAndSystemCode(config.getDataSourceName(), config.getSystemCode());
        String key = null;
        if (Objects.isNull(id)) {
            dataSourceConfigDao.insertSelective(config);
            key = dataSourceInitListener.addDataSource(config);
        }
        return key;
    }

    @Override
    public void updateDateSource(DownloadDataSourceConfig config) {
        dataSourceConfigDao.updateByPrimaryKeySelective(config);
    }
}
